/*
 * Copyright © Yan Zhenjie. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.iotimc.attendancesystem.server.response;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.iotimc.attendancesystem.entity.PersonEntity;
import com.iotimc.networklibrary.entity.BaseEntity;
import com.yanzhenjie.andserver.RequestHandler;
import com.yanzhenjie.andserver.util.HttpRequestParser;

import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HttpContext;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author david
 */
public class RequestSyncHandler implements RequestHandler {

    @Override
    public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {

        Map<String, String> params = HttpRequestParser.parse(request);
        String json = params.get("json");
        List<PersonEntity> personEntities = JSON.parseArray(json, PersonEntity.class);
        Log.i("AndServer", "Params: " + personEntities.size());
        BaseEntity<String> baseEntity = new BaseEntity<>();
        baseEntity.setMessage("同步成功");
        baseEntity.setStatus("0");
        baseEntity.setResult("请求成功" + personEntities.size());
        baseEntity.setTime(System.currentTimeMillis());
        StringEntity stringEntity = new StringEntity(JSON.toJSONString(baseEntity), "utf-8");
        response.setEntity(stringEntity);
    }
}
